package com.example.cloudprinter.printer.feie;

import com.example.cloudprinter.api.feie.FEIEPrinterApi;
import com.example.cloudprinter.network.retrofit.RetrofitUtil;
import com.example.cloudprinter.printer.feie.callback.OnAddCallback;
import com.example.cloudprinter.printer.feie.callback.OnDeleteCallback;
import com.example.cloudprinter.printer.feie.callback.OnPrintCallback;
import com.example.cloudprinter.printer.feie.callback.OnStateCallback;
import com.example.cloudprinter.network.RxUtil;
import com.example.cloudprinter.utils.SHA;

import rx.subscriptions.CompositeSubscription;

/**
 * 飞鹅打印机
 */
public class FeiePrinter {
    private static final String host = "http://api.feieyun.cn/";
    private static final String user = "1357103059@qq.com";
    private static final String ukey = "S5DY5E8QSq25PxVF";
    private FEIEPrinterApi api;

    private String stime = String.valueOf(System.currentTimeMillis() / 1000);

    private CompositeSubscription mSubscriptions = new CompositeSubscription();

    public FeiePrinter() {
        api = RetrofitUtil.getService(FEIEPrinterApi.class, host);
    }

    /**
     * 添加打印机
     * （一台打印机设备可以被多个应用平台绑定）
     *
     * @param sn       打印机编号
     * @param key      打印机密钥
     * @param alias    打印机别名
     * @param callback
     */
    public void addPrinter(String sn, String key, String alias, OnAddCallback callback) {
        String printerContent = sn + " # " + key + " # " + alias + " # 00000000000";
        mSubscriptions.add(api.addPrinter(user, stime, SHA.encryptToSHA(user + ukey + stime),
                printerContent, "Open_printerAddlist")
                .compose(RxUtil.applySchedulers())
                .subscribe(addResponse -> {
                    if (callback != null) {
                        if (addResponse.getRet() == 0) {
                            callback.onAddSuccess();
                        } else {
                            callback.onAddFailure(addResponse.getRet(), addResponse.getMsg());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onAddFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 删除打印机
     *
     * @param sn       打印机编号
     * @param callback
     */
    public void deletePrinter(String sn, OnDeleteCallback callback) {
        mSubscriptions.add(api.deletePrinter(user, stime, SHA.encryptToSHA(user + ukey + stime),
                "Open_printerDelList", sn)
                .compose(RxUtil.applySchedulers())
                .subscribe(addResponse -> {
                    if (callback != null) {
                        if (addResponse.getRet() == 0) {
                            callback.onDeleteSuccess();
                        } else {
                            callback.onDeleteFailure(addResponse.getRet(), addResponse.getMsg());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onDeleteFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 获取打印机状态
     *
     * @param sn       打印机编号
     * @param callback
     */
    public void getPrinterState(String sn, OnStateCallback callback) {
        mSubscriptions.add(api.getPrinterState(user, stime, SHA.encryptToSHA(user + ukey + stime),
                "Open_queryPrinterStatus", sn)
                .compose(RxUtil.applySchedulers())
                .subscribe(stateResponse -> {
                    if (callback != null) {
                        if (stateResponse.getRet() == 0) {
                            callback.onState(stateResponse.getData());
                        } else {
                            callback.onQuestFailure(stateResponse.getRet(), stateResponse.getMsg());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onQuestFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 打印
     *
     * @param sn       打印机编号
     * @param content  打印内容
     * @param times    打印次数
     * @param callback
     */
    public void print(String sn, String content, String times, OnPrintCallback callback) {
        mSubscriptions.add(api.print(user, stime, SHA.encryptToSHA(user + ukey + stime),
                "Open_printMsg", sn, content, times)
                .compose(RxUtil.applySchedulers())
                .subscribe(printResponse -> {
                    if (callback != null) {
                        if (printResponse.getRet() == 0) {
                            callback.onPrintSuccess(content);
                        } else {
                            callback.onPrintFailure(printResponse.getRet(), printResponse.getMsg());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onPrintFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 移除所有的请求
     */
    public void release() {
        mSubscriptions.clear();
    }

}
